package com.syp.hehe.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.syp.hehe.util.ProcessResult;
import com.syp.hehe.util.SessionInfo;
import com.syp.hehe.web.WebappRetCodeEnum;

@Service
public class SessionInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute("sessionInfo");
		if(sessionInfo != null){
			return true;
		}else{
			ProcessResult pr = new ProcessResult();
			pr.setSuccess(false);
			pr.setMessage("session不存在。");
			pr.setRetCode(WebappRetCodeEnum.NO_SESSION.getCode());
			
			response.reset();
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(JSON.toJSONString(pr));
		}
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		/**
		 * do nothing
		 */
		
	}
	

}
